package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.AlterRefundInfo;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.order.common.CheckRefundUtils;
import com.vogue.supplymall.order.domain.biz.RefundBiz;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Refund update controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-03-19
 */
@RestController
public class RefundUpdateController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger log = LoggerFactory.getLogger(RefundUpdateController.class);

    @Resource
    private RefundBiz refundBiz;
    @Resource
    private MessageSourceUtils messageSourceUtils;

    /**
     * Upload refund img response entity.
     *
     * @param appver     the appver
     * @param refundInfo the refund info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/refund/update/status", method = RequestMethod.POST)
    public ResponseEntity<Object> updateRefundStatus(@PathVariable("appver") String appver, @RequestBody AlterRefundInfo refundInfo) {
        log.info("update Refund status start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (CheckRefundUtils.checkUpdateRefundParameter(refundInfo, info.getLoginUserType())) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(refundBiz.updateRefund(refundInfo, info));
        resultInfo.setData(null);
        /* 返回订单修改成功 */
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< update Refund status end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    /**
     * Update refund fulfillment response entity.
     *
     * @param appver     the appver
     * @param refundInfo the refund info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/refund/update/express", method = RequestMethod.POST)
    public ResponseEntity<Object> updateRefundFulfillment(@PathVariable("appver") String appver, @RequestBody AlterRefundInfo refundInfo) {
        log.info("update Refund express start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (null == refundInfo.getExpress() || null == refundInfo.getRefundid()) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(refundBiz.updateFulfillment(refundInfo));
        /* 返回订单修改成功 */
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< update Refund express end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
    @RequestMapping(value = "/is/{appver}/order/refund/refundmode/{orderid}", method = RequestMethod.POST)
    public ResponseEntity<Object> refundMode(@PathVariable("appver") String appver, @PathVariable("orderid")Long orderid) {
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(refundBiz.refundMode(orderid));
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
